import Vue from 'vue'
import Router from 'vue-router'
import userController from '@/api/userController/index'
import Home from '@/views/dabashed/index'
import Search from '@/views/search/index'
import Bookshelf from '@/views/bookshelf/index'
import Classify from '@/views/classify/index'
import Community from '@/views/community/index'
import Free from '@/views/free/index'
import Monthly from '@/views/monthly/index'
import Ranking from '@/views/ranking/index'
import Sign from '@/views/sign/index'
import Write from '@/views/write/index'
import FenleiXq from '@/views/classify/xq'
import bookinfo from '@/views/bookinfo/index'
import login from '@/views/login/index'
import register from '@/views/register/index'
import userCenter from '@/views/usercenter/index'
import userinfo from '@/views/usercenter/userinfo'
import paycenter from '@/views/usercenter/paycenter'
import paywx from '@/views/usercenter/paywx'
import payzfb from '@/views/usercenter/payzfb'
import ktbaoyue from '@/views/usercenter/baoyue'
import paybaoyue from '@/views/usercenter/paybaoyue'
import voucher from '@/views/usercenter/voucher'
import mycomment from '@/views/usercenter/comment'
import commentReply from '@/views/usercenter/commentReply'
import mynews from '@/views/usercenter/mynews'
import authorCenter from '@/views/authorCenter/index'
import penName from '@/views/authorCenter/createPenName'
import createBookPage from '@/views/authorCenter/createBookPage'
import createBookChapterPage from '@/views/authorCenter/createBookChapterPage'
import library from '@/views/authorCenter/library'
import getManageBookPage from '@/views/authorCenter/getManageBookPage'
import updateChapter from '@/views/authorCenter/updateChapter'
import findPassWordCode from '@/views/usercenter/findPassWordCode'
import resetPassword from '@/views/usercenter/resetPassword'

import jsCookie from 'js-cookie'
import store from '@/store'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
      alias: '/home',
      meta: {
        keepAlive: true,
        code: 'home',
        title:'首页'
      }
    },
    {
      path: '/search',
      name: 'Search',
      component: Search,
      alias: '/tosearch',
      meta: {
        keepAlive: false,
        code: 'search',
        title:'搜索'
      }
    },
    {
      path: '/bookshelf',
      name: 'Bookshelf',
      component: Bookshelf,
      alias: '/shujia',
      meta: {
        keepAlive: false,
        code: 'bookshelf',
        title:'我的书架'
      }
    },
    {
      path: '/classify',
      name: 'Classify',
      component: Classify,
      alias: '/fenlei',
      meta: {
        keepAlive: true,
        code: 'classify',
        title:'分类'
      }
    },
    {
      path: '/community',
      name: 'Community',
      component: Community,
      alias: '/shequ',
      meta: {
        keepAlive: false,
        code: 'community',
        title:'社区'
      }
    },
    {
      path: '/free',
      name: 'Free',
      component: Free,
      alias: '/mianfei',
      meta: {
        keepAlive: true,
        code: 'free',
        title:'免费频道'
      }
    },
    {
      path: '/monthly',
      name: 'Monthly',
      component: Monthly,
      alias: '/baoyue',
      meta: {
        keepAlive: true,
        code: 'monthly',
        title:'包月书库'
      }
    },
    {
      path: '/ranking',
      name: 'Ranking',
      component: Ranking,
      alias: '/paiming',
      meta: {
        keepAlive: true,
        code: 'ranking',
        title:'排行榜'
      }
    },
    {
      path: '/sign',
      name: 'Sign',
      component: Sign,
      alias: '/qiandao',
      meta: {
        keepAlive: false,
        code: 'sign',
        title:'书写'
      }
    },
    {
      path: '/write',
      name: 'Write',
      component: Write,
      alias: '/shuxie',
      meta: {
        keepAlive: false,
        code: 'write',
        title:'签到'
      }
    },
    {
      path: '/fenleiXq/:fenleiTabId/:fenleixqId',
      name: 'FenleiXq',
      component: FenleiXq,
      alias: '/fenleixiangqin',
      meta: {
        keepAlive: false,
        code: 'fenleiXq',
        title:'分类详情'
      }
    },
    {
      path: '/bookinfo/:bookid',
      name: 'bookinfo',
      component: bookinfo,
      alias: '/shuxinxi',
      meta: {
        keepAlive: false,
        code: 'bookinfo',
        title:'书籍详情'
      }
    },
    {
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        keepAlive: false,
        code: 'login',
        title:'登陆'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: register,
      meta: {
        keepAlive: false,
        code: 'register',
        title:'注册'
      }
    },
    {
      path:'/usercenter',
      name:'usercenter',
      component: userCenter,
      meta: {
        keepAlive: false,
        code: 'usercenter',
        title:'个人中心'
      }
    },
    {
      path:'/penName',
      name:'penName',
      component: penName,
      meta: {
        keepAlive: false,
        code: 'penName',
        title: '成为作者'
      }
    },
    {
      path:'/userinfo',
      name:'userinfo',
      component: userinfo,
      meta: {
        keepAlive: false,
        code: 'userinfo',
        title:'个人资料'
      },
    },
    {
      path:'/paycenter',
      name:'paycenter',
      component: paycenter,
      meta: {
        keepAlive: false,
        code: 'paycenter',
        title:'充值中心'
      }
    },
    {
      path:'/paywx',
      name:'paywx',
      component: paywx,
      meta: {
        keepAlive: true,
        code: 'paywx',
        title:'微信支付'
      }
    },
    {
      path:'/payzfb',
      name:'payzfb',
      component: payzfb,
      meta: {
        keepAlive: true,
        code: 'payzfb',
        title:'支付宝支付'
      }
    },
    {
      path:'/ktbaoyue',
      name:'ktbaoyue',
      component: ktbaoyue,
      meta: {
        keepAlive: false,
        code: 'ktbaoyue',
        title:'开通包月'
      }
    },
    {
      path:'/paybaoyue',
      name:'paybaoyue',
      component: paybaoyue,
      meta: {
        keepAlive: false,
        code: 'paybaoyue',
        title:'开通包月'
      }
    },
    {
      path:'/voucher',
      name:'voucher',
      component: voucher,
      meta: {
        keepAlive: false,
        code: 'voucher',
        title:'我的代金卷'
      }
    },
    {
      path:'/mycomment',
      name:'mycomment',
      component: mycomment,
      meta: {
        keepAlive: false,
        code: 'mycomment',
        title:'我的书评'
      }
    },
    {
      path:'/commentReply/:commentId',
      name:'commentReply',
      component: commentReply,
      meta: {
        keepAlive: false,
        code: 'commentReply',
        title:'回复'
      }
    },
    {
      path:'/mynews',
      name:'mynews',
      component: mynews,
      meta: {
        keepAlive: false,
        code: 'mynews',
        title:'我的消息'
      }
    },
    {
      path:'/authorCenter',
      name:'authorCenter',
      component: authorCenter,
      meta: {
        keepAlive: false,
        code: 'authorCenter',
        title:'作者中心'
      }
    },
    {
      path:'/createBookPage',
      name:'createBookPage',
      component: createBookPage,
      meta: {
        keepAlive: false,
        code: 'createBookPage',
        title:'创建新书'
      }
    },
    {
      path:'/createBookChapterPage',
      name:'createBookChapterPage',
      component: createBookChapterPage,
      meta: {
        keepAlive: false,
        code: 'createBookChapterPage',
        title:'新建章节'
      }
    },
    {
      path:'/library',
      name:'library',
      component: library,
      meta: {
        keepAlive: false,
        code: 'library',
        title:'我的作品库'
      }
    },
    {
      path:'/getManageBookPage',
      name:'getManageBookPage',
      component: getManageBookPage,
      meta: {
        keepAlive: false,
        code: 'getManageBookPage',
        title:'作品管理'
      }
    },
    {
      path:'/findPassWordCode',
      name:'findPassWordCode',
      component: findPassWordCode,
      meta: {
        keepAlive: false,
        code: 'findPassWordCode',
        title:'找回密码'
      }
    },    
    {
      path:'/resetPassword/:code',
      name:'resetPassword',
      component: resetPassword,
      meta: {
        keepAlive: false,
        code: 'resetPassword',
        title:'重置密码'
      }
    },   
    {
      path:'/updateChapter',
      name:'updateChapter',
      component: updateChapter,
      meta: {
        keepAlive: false,
        code: 'updateChapter',
        title:'修改章节'
      }
    },           
    
  ]
})
export default router
router.beforeEach((to, from, next) => {
  store.dispatch("updateLoadingStatus",{isLoading:true})
  userController.getLoginUserInfo().then(response=>{//获取用户信息
    //设置用户信息
    store.dispatch('setNowPage',to);
    store.dispatch('setUserInfo',response.data);
    next();
  }).catch(()=>{ //网络请求错误
    let res = {
      isLogin:'1',
      success:false
    }
     return res
     next();
  })
})
router.afterEach(function (to) {
  store.dispatch('updateLoadingStatus', {isLoading: false});
  window.scrollTo(0,0);
})

